package pdfsetter.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
public class PDFElementPanel extends JPanel {
	
//	private final PDFImageComponent pdfImage;
//	private final PDFElement element;
	
	public PDFElementPanel(final PDFImageComponent pdfImage, final PDFElement element) {
//		this.pdfImage = pdfImage;
//		this.element = element;
		setLayout(new BorderLayout());
		setMaximumSize(new Dimension((int) pdfImage.getSize().getWidth(), (int) pdfImage.getSize().getHeight() + 50));
		JLabel label = new JLabel(element.name);
		add(label, BorderLayout.PAGE_START);
		add(pdfImage, BorderLayout.CENTER);
		final JTextArea pageNoField = new JTextArea(1, 3);
		pageNoField.setText("1");
		pageNoField.setMaximumSize(new Dimension(30, 15));
		add(pageNoField, BorderLayout.PAGE_END);
		JButton changePageButton = new JButton("wybierz");
		changePageButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int pageNo = 0;
				try {
					pageNo = Integer.parseInt(pageNoField.getText()) - 1;
					if(pageNo < 0 || pageNo >= element.pages.size())
						throw new NumberFormatException();
				}
				catch(NumberFormatException e) {
					pageNo = 0;
				}
				pageNoField.setText("" + (pageNo+1));
				pdfImage.changePage(pageNo);
				element.pageNo = pageNo;
			}
		});
		Box box = Box.createHorizontalBox();
		box.add(new JLabel("strona (" + element.pages.size() + "): "));
		box.add(pageNoField);
		box.add(changePageButton);
		add(box, BorderLayout.PAGE_END);
	}

}
